package com.example.task;

import com.example.mapper.MessageMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class CleanMessage {

    @Resource
    private MessageMapper messageMapper;
    @Scheduled(cron = "0 0/30 * * * ? ") //每30分钟检查一次
    public void cleanMessage() {
        log.info("开始删除超过3天的消息...");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime threeDaysAgo = now.minusDays(3);

        // 删除超过15天的消息
        int deletedCount = messageMapper.deleteOldMessages(
                threeDaysAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        );

      log.info("删除超过3天的消息，共删除{}条", deletedCount);
    }
}
